package com.quanyan.ic.vendingMachine.http.service.impl;

import com.quanyan.ic.common.spring.SpringUtil;
import com.quanyan.ic.vendingMachine.http.service.VmUploadService;
import com.quanyan.ic.vendingMachine.protocol.Cmd;
import com.quanyan.ic.vendingMachine.protocol.codec.protobuf.VmMessageBuilder;
import com.quanyan.ic.vendingMachine.protocol.codec.protobuf.VmMessageProto;
import com.quanyan.ic.vendingMachine.protocol.server.session.Session;
import com.quanyan.ic.vendingMachine.protocol.server.session.SessionManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by zhoujin on 2017/8/22.
 */
@Service
public class VmUploadServiceImpl  implements VmUploadService{
    private static Logger logger = LoggerFactory.getLogger(VmUploadServiceImpl.class);

    /**
     * 获取client上传的日志文件并保存在服务器
     * @param file 文件
     * vmNumber 售货机编号
     */
    @Override
    public void upLoadLogFile(MultipartFile file, String vmNumber) {
        String fileName = file.getOriginalFilename();
        File filePath = new File("/Users/zhoujin/Desktop/larklog"+"/"+vmNumber);
        if(!filePath.exists()){
            filePath.mkdirs();
        }
        File targetFile = new File(filePath, fileName);
        if(!targetFile.exists()){
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }






    /**
     * 发送获取指定日期和指定售货机编号的日志
     * @param  vmNumber 售货机编号
     * @param  time     日期
     */
    public  void sendUploadLogCmd(final String vmNumber, String time) {
        SessionManager sessionManager = SpringUtil.getBean("sessionManager",SessionManager.class);
        Session session = sessionManager.get(vmNumber);
        if(session == null || session.getChannel() == null){
            logger.warn("---> send cmd [{}] to vm [{}], client offline", Cmd.UPLOADLOG_REQ.getDesc(),vmNumber);
            return;
        }
        Channel channel = session.getChannel();
        final VmMessageProto.VmMessage vmMessage = new VmMessageBuilder(vmNumber)
                .buildCmd(Cmd.UPLOADLOG_REQ)
                .buildLongTime(time)
                .getMessage();
        ChannelFuture future  = channel.writeAndFlush(vmMessage);
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                logger.info("---> send cmd [{}] to vm [{}] completed, d={} c={} s={}， msg=\n{}",
                        Cmd.UPDATE_GOODSLIST_REQ.getDesc(), vmNumber, channelFuture.isDone(), channelFuture.isCancelled(), channelFuture.isSuccess(), vmMessage);
            }
        });
    }


}
